﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

public partial class NickInfo : System.Web.UI.Page
{
    int pageNums = 0;
    int counts = 0;
    int currentPage = 1;
    int pageSize = 30;
    System.Data.DataSet ds = new System.Data.DataSet();
    TB.BLL.Dianpu dp = new TB.BLL.Dianpu();
    TB.BLL.UserName user = new TB.BLL.UserName();
    TB.BLL.VisitLog logBll = new TB.BLL.VisitLog();
    string privilege = "";
    string ieUrl = "";
    string conditionWhere = "";
    string loginUser = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        ieUrl = Request.Url.ToString();
        if (!IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }
        ((Label)this.Master.FindControl("lblCurrentUser")).Text = Session["username"].ToString();
        loginUser = Session["username"].ToString();
        privilege = user.GetUserPrivilege(Session["username"].ToString(), Session["pwd"].ToString());
        if (ieUrl.Contains("?"))
        {
            NameValueCollection nvc = Request.QueryString;
            if (ieUrl.Contains("pnum"))
            {
                currentPage = Convert.ToInt32(nvc.GetValues("pnum")[0]);
                for (int i = 0; i < nvc.Count; i++)
                {
                    if (nvc.AllKeys[i].ToString() != "pnum")
                        conditionWhere = conditionWhere + GetColsName(nvc.AllKeys[i]) + "='" + nvc.GetValues(i)[0] + "'" + " and ";
                }
                conditionWhere = conditionWhere.TrimEnd(' ', 'a', 'n', 'd');
                this.lblPageIndex.Text = currentPage.ToString();
                ds = dp.GetPages(currentPage, pageSize, out counts, out pageNums, conditionWhere);
                grvInfo.DataSource = ds;
                this.lblPageCount.Text = pageNums.ToString();
                this.lblRecNum.Text = counts.ToString();
                grvInfo.DataBind();
            }
            else
            {
                for (int i = 0; i < nvc.Count; i++)
                {
                    if (nvc.AllKeys[i].ToString() != "pnum")
                        conditionWhere = conditionWhere + GetColsName(nvc.AllKeys[i]) + "='" + nvc.GetValues(i)[0] + "'" + " and ";
                }
                conditionWhere = conditionWhere.TrimEnd(' ', 'a', 'n', 'd');
                conditionWhere = conditionWhere.TrimEnd(' ', 'a', 'n', 'd');
                this.lblPageIndex.Text = currentPage.ToString();
                ds = dp.GetPages(currentPage, pageSize, out counts, out pageNums, conditionWhere);
                grvInfo.DataSource = ds;
                this.lblPageCount.Text = pageNums.ToString();
                this.lblRecNum.Text = counts.ToString();
                grvInfo.DataBind();
            }
        }
        else
        {
            this.lblPageIndex.Text = currentPage.ToString();
            ds = dp.GetPages(currentPage, pageSize, out counts, out pageNums);
            grvInfo.DataSource = ds;
            this.lblPageCount.Text = pageNums.ToString();
            this.lblRecNum.Text = counts.ToString();
            grvInfo.DataBind();
        }
    }

    private string GetColsName(string key)
    {
        if (key == "ct")
            return "createTime";
        if (key == "sj")
            return "sjName";
        if (key == "ver")
            return "dpVer";
        if (key == "hy")
            return "hanye";
        if (key == "pnum")
            return "pnum";
        if (key == "xy")
            return "dpXy";
        return "";
    }

    protected void grvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            string nick = ds.Tables[0].Rows[e.Row.DataItemIndex]["nickname"].ToString();
            string nickname = System.Web.HttpUtility.UrlEncode(ds.Tables[0].Rows[e.Row.DataItemIndex]["nickname"].ToString());
            string userId = ds.Tables[0].Rows[e.Row.DataItemIndex]["userId"].ToString();
            string dpxy = ds.Tables[0].Rows[e.Row.DataItemIndex]["dpXy"].ToString();

            HyperLink linkNickName = (HyperLink)e.Row.Cells[1].FindControl("linkNickName");
            Image imgNickName = (Image)e.Row.Cells[1].FindControl("imgNickName");
            HyperLink linkVisiteNum = (HyperLink)e.Row.FindControl("lblVisitedNum");
            
            HyperLink linkDpXy = (HyperLink)e.Row.Cells[2].FindControl("linkdpXy");
           // Image xyImg = (Image)e.Row.Cells[2].FindControl("xyImg");
            ImageButton imgDpXy = (ImageButton)e.Row.Cells[1].FindControl("imgdpXy");
            //imgDpXy.Text = dpxy;

            LinkButton btnLinkCreateTime = (LinkButton)e.Row.Cells[1].FindControl("btnLinkCreateTime");
            string createTime=ds.Tables[0].Rows[e.Row.DataItemIndex]["createTime"].ToString().Split(' ')[0];

            LinkButton btnLinkHy = (LinkButton)e.Row.Cells[1].FindControl("btnLinkHy");
            string hanye = ds.Tables[0].Rows[e.Row.DataItemIndex]["hanye"].ToString();

            

            LinkButton btnLinkVer = (LinkButton)e.Row.Cells[1].FindControl("btnLinkVer");
            string dpVer = ds.Tables[0].Rows[e.Row.DataItemIndex]["dpVer"].ToString();

            LinkButton btnLinkSjName = (LinkButton)e.Row.Cells[1].FindControl("btnLinkSjName");
            string sjName = ds.Tables[0].Rows[e.Row.DataItemIndex]["sjName"].ToString();

            if (linkNickName != null && imgNickName != null && linkDpXy!=null)
            {
                
                linkDpXy.NavigateUrl = "http://rate.taobao.com/user-rate-" + userId + ".htm";
                linkDpXy.Attributes["target"] = "_blank";
                if (dpxy == "<11")
                    dpxy = "low11.gif";
                if (dpxy == "0")
                    dpxy = "mao.gif";
                imgDpXy.ImageUrl = "images/" + dpxy;

                if (privilege == "1")
                {
                    linkNickName.NavigateUrl = "http://www.taobao.com/webww/ww.php?ver=3&touid=" + nickname + "&siteid=cntaobao&status=1&charset=utf-8";
                    linkNickName.Attributes["target"] = "_blank";
                    imgNickName.Attributes["alt"] = "给我留言";
                    imgNickName.Attributes["src"] = "http://amos.alicdn.com/online.aw?v=2&uid=" + nickname + "&site=cntaobao&s=1&charset=utf-8";
                    imgNickName.Attributes["border"] = "0";

                    btnLinkCreateTime.Text = createTime;
                    btnLinkHy.Text = hanye;
                    btnLinkVer.Text = dpVer;
                    btnLinkSjName.Text = sjName;

                    linkVisiteNum.Text = logBll.GetVisitedNum(loginUser, nick);
                    linkVisiteNum.NavigateUrl = "visiteLog.aspx?us=" + loginUser + "&nick=" + nickname;
                }
                else
                {
                    imgNickName.Attributes["alt"] = "给我留言";
                    imgNickName.Attributes["src"] = "http://amos.alicdn.com/online.aw?v=2&uid=" + nickname + "&site=cntaobao&s=1&charset=utf-8";
                    imgNickName.Attributes["border"] = "0";

                    btnLinkCreateTime.Text = createTime;
                    btnLinkCreateTime.Enabled = false;

                    btnLinkHy.Text = hanye;
                    btnLinkHy.Enabled = false;

                    btnLinkVer.Text = dpVer;
                    btnLinkVer.Enabled = false;

                    btnLinkSjName.Text = sjName;
                    btnLinkSjName.Enabled = false;
                }
            }
        }
    }

    protected void btn_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandArgument.ToString() == "First")
        {
            if (this.lblPageIndex.Text == "1")
                this.btnFirst.Enabled = false;
            else
            {
                string url = CombineURL(ieUrl, "pnum", "1");
                Response.Redirect(url);
            }
        }
        else if (e.CommandArgument.ToString() == "Prev")
        {
            int currentPage = Convert.ToInt32(this.lblPageIndex.Text) -1;
            if (currentPage != 0)
            {
                string url = CombineURL(ieUrl, "pnum", currentPage.ToString());
                Response.Redirect(url);
            }
            else
            {
                this.btnPrev.Enabled = false;
            }
        }
        else if (e.CommandArgument.ToString() == "Next")
        {
            int currentPage = Convert.ToInt32(this.lblPageIndex.Text)+1;
            if (currentPage > Convert.ToInt32(this.lblPageCount.Text))
            {
                this.btnNext.Enabled = false;
            }
            else
            {
                string url = CombineURL(ieUrl, "pnum", currentPage.ToString());
                Response.Redirect(url);
            }
        }
        else if (e.CommandArgument.ToString() == "Last")
        {
            if (this.lblPageIndex.Text == this.lblPageCount.Text)
                this.btnLast.Enabled = false;
            else
            {
                string url = CombineURL(ieUrl, "pnum", this.lblPageCount.Text);
                Response.Redirect(url);
            }
        }
        else
        {
            if (Convert.ToInt32(this.txtNewPageIndex.Text) > Convert.ToInt32(this.lblPageCount.Text))
            {
                this.btnGo.Enabled = false;
            }
            else
            {
                string url = CombineURL(ieUrl, "pnum", this.txtNewPageIndex.Text);
                Response.Redirect(url);
            }
        }
    }

    protected void grvInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandArgument.ToString() == "createTime")
        {
            string url = CombineURL(ieUrl, "ct", ((LinkButton)e.CommandSource).Text);
            Response.Redirect(url);
        }
        if (e.CommandArgument.ToString() == "hanye")
        {
            string url = CombineURL(ieUrl, "hy", ((LinkButton)e.CommandSource).Text);
            Response.Redirect(url);
        }
        if (e.CommandArgument.ToString() == "dpVer")
        {
            string url = CombineURL(ieUrl, "ver", ((LinkButton)e.CommandSource).Text);
            Response.Redirect(url);
        }
        if (e.CommandArgument.ToString() == "sjName")
        {
            string url = CombineURL(ieUrl, "sj", ((LinkButton)e.CommandSource).Text);
            Response.Redirect(url);
        }
        if (e.CommandName.ToString() == "imgdpXy")
        {
            string xyRank = ((ImageButton)e.CommandSource).ImageUrl.Split('/')[1];
            if (xyRank == "low11.gif")
                xyRank = "<11";
            if (xyRank == "mao.gif")
                xyRank = "0";
            string url = CombineURL(ieUrl, "xy", xyRank);
            Response.Redirect(url);
        }
    }
    /// <summary>
    /// 组合访问的URL串。http://localhost:19596/WebSite/NickInfo.aspx?pnum=40&id=1000
    /// </summary>
    /// <param name="url"></param>
    /// <param name="kind"></param>
    /// <returns></returns>
    public string CombineURL(string rawUrl, string kind,string value)
    {
        string url="";
        if (rawUrl.Contains("?")) //表明已含有一个参数。
        {
            if (!rawUrl.Contains(kind))
                url = rawUrl + "&" + kind + "=" + System.Web.HttpUtility.UrlEncode(value);
            else
            {
                string[] temp=rawUrl.Split('?','=','&');
                int indexPos = 0;
                for (int i=0;i<temp.Length;i++)
                {
                    if (temp[i] == kind)
                    {
                        indexPos = i;
                        break;
                    }
                }
                url=rawUrl.Replace(temp[indexPos + 1], value);
            }       
        }
        else
        {
            url=rawUrl + "?" + kind + "=" + System.Web.HttpUtility.UrlEncode(value);
        }
        return url;
    }
}